#!/usr/bin/perl -w
#purpose: RRTs combining %GC matching and codon pair bias; read codon usage from output of CodonPrintGC3

$dnafile = $ARGV[0];#fasta formatted DNA file (sequence all on 1 line)
open(DNA,$dnafile);

$CIGC3 = $ARGV[1];#output from CodonPrintGC3.pl
open(CIFILE,$CIGC3);

$output = $ARGV[2];
open(OUTPUT,">$output");

$rrts = 200;#number of RRTs to make for each sequence
$SeqCutoff = 50;#only use codon usage bins with data from at least this many sequences
$GCcutoff = 1;#how close RRT %GC should be to WT %GC
$end = 5;#if can't make RRT for seq within this GC range, quit
$max_attempts = 50; #If RRT can't be generated in 50 tries abandon it

$CIbins = 0;#how many CI bins will be used
@GC3Ave = ();#average %GC3 for each bin used

#these will become arrays of codons:
@F = ();@L = ();@I = ();@M = ();@V = ();@S = ();@P = ();@T = ();@A = ();@Y = ();@X = ();@H = ();@Q = ();@N = ();@K = ();@D = ();@E = ();@C = ();@W = ();@R = ();@G = ();
#lengths of codon strings to set limits on random numbers:
@Flengths = ();@Llengths = ();@Ilengths = ();@Mlengths = ();@Vlengths = ();@Slengths = ();@Plengths = ();@Tlengths = ();@Alengths = ();@Ylengths = ();@Hlengths = ();@Qlengths = ();@Nlengths = ();@Klengths = ();@Dlengths = ();@Elengths = ();@Clengths = ();@Wlengths = ();@Rlengths = ();@Glengths = ();@Xlengths = ();

#arrays of codons for choosing without codon pair bias: for 3' ends of sequences
@Ftotals = ();@Ltotals = ();@Itotals = ();@Mtotals = ();@Vtotals = ();@Stotals = ();@Ptotals = ();@Ttotals = ();@Atotals = ();@Ytotals = ();@Xtotals = ();@Htotals = ();@Qtotals = ();@Ntotals = ();@Ktotals = ();@Dtotals = ();@Etotals = ();@Ctotals = ();@Wtotals = ();@Rtotals = ();@Gtotals = ();
@Ftlengths = ();@Ltlengths = ();@Itlengths = ();@Mtlengths = ();@Vtlengths = ();@Stlengths = ();@Ptlengths = ();@Ttlengths = ();@Atlengths = ();@Ytlengths = ();@Htlengths = ();@Qtlengths = ();@Ntlengths = ();@Ktlengths = ();@Dtlengths = ();@Etlengths = ();@Ctlengths = ();@Wtlengths = ();@Rtlengths = ();@Gtlengths = ();@Xtlengths = ();


#binnum\tseqs\taveGC\tAA\tcodons
@codonfile = <CIFILE>;
chomp @codonfile;
shift @codonfile;

$lastbin = -1;
foreach $line (@codonfile){

	@line = split("\t",$line);
	$binID = shift @line;
	$seqs = shift @line;
	$avegc3 = shift @line;
	$aa = shift @line;
	
	unless ($binID =~ "TOTALS" or $aa eq "X"){#"TOTALS" indicates line has total codon counts (no codon pair bias);X lines = stop codon
		if ($seqs > $SeqCutoff or $seqs == $SeqCutoff){
		 	
			 $codonset1 = shift @line;#set of codon strings for first codon of synonymous set
			 @codons = split(" ",$codonset1);#each element is codon string for codon1 before a different 3' codon
		 	
			 until (scalar @line == 0){
			 	$codonset = shift @line;#set of codon strings for next codon of synonymous set
			 	@nextcodon = split(" ",$codonset);#each element is codon string for next synonymous codon before a different 3' codon
			 	#add together strings of synonymous codons: sets for each 3' codon go together
			 	$codon3p = 0;
			 	foreach $set (@nextcodon){
			 		$codons[$codon3p] = $codons[$codon3p] . $set;
			 		++$codon3p;
			 	}#foreach codon set, add to 3' set for this AA/GC bin combo
			 }#until collected other synonymous codons for AA
		 	 
			 #add codon data for bin to AA array:
			if ($aa eq "F"){push(@F,[@codons]);}
			elsif ($aa eq "L"){push(@L,[@codons]);}
			elsif ($aa eq "I"){push(@I,[@codons]);}
			elsif ($aa eq "M"){push(@M,[@codons]);}
			elsif ($aa eq "V"){push(@V,[@codons]);}
			elsif ($aa eq "S"){push(@S,[@codons]);}
			elsif ($aa eq "P"){push(@P,[@codons]);}
			elsif ($aa eq "T"){push(@T,[@codons]);}
			elsif ($aa eq "A"){push(@A,[@codons]);}
			elsif ($aa eq "Y"){push(@Y,[@codons]);}
			elsif ($aa eq "H"){push(@H,[@codons]);}
			elsif ($aa eq "Q"){push(@Q,[@codons]);}
			elsif ($aa eq "N"){push(@N,[@codons]);}
			elsif ($aa eq "K"){push(@K,[@codons]);}
			elsif ($aa eq "D"){push(@D,[@codons]);}
			elsif ($aa eq "E"){push(@E,[@codons]);}
			elsif ($aa eq "C"){push(@C,[@codons]);}
			elsif ($aa eq "W"){push(@W,[@codons]);}
			elsif ($aa eq "R"){push(@R,[@codons]);}
			elsif ($aa eq "G"){push(@G,[@codons]);}
			elsif ($aa eq "X"){push(@X,[@codons]);}
			
			unless ($binID == $lastbin){++$CIbins;$lastbin = $binID;push(@GC3Ave,$avegc3);}
			 #record codon lengths to set limits on random number thingy later
			 @lengths = ();
			 foreach $codonstring (@codons){$length = length $codonstring;$cdns = $length/3;push(@lengths,$cdns);}
			if ($aa eq "F"){push(@Flengths,[@lengths]);}
			elsif ($aa eq "L"){push(@Llengths,[@lengths]);}
			elsif ($aa eq "I"){push(@Ilengths,[@lengths]);}
			elsif ($aa eq "M"){push(@Mlengths,[@lengths]);}
			elsif ($aa eq "V"){push(@Vlengths,[@lengths]);}
			elsif ($aa eq "S"){push(@Slengths,[@lengths]);}
			elsif ($aa eq "P"){push(@Plengths,[@lengths]);}
			elsif ($aa eq "T"){push(@Tlengths,[@lengths]);}
			elsif ($aa eq "A"){push(@Alengths,[@lengths]);}
			elsif ($aa eq "Y"){push(@Ylengths,[@lengths]);}
			elsif ($aa eq "H"){push(@Hlengths,[@lengths]);}
			elsif ($aa eq "Q"){push(@Qlengths,[@lengths]);}
			elsif ($aa eq "N"){push(@Nlengths,[@lengths]);}
			elsif ($aa eq "K"){push(@Klengths,[@lengths]);}
			elsif ($aa eq "D"){push(@Dlengths,[@lengths]);}
			elsif ($aa eq "E"){push(@Elengths,[@lengths]);}
			elsif ($aa eq "C"){push(@Clengths,[@lengths]);}
			elsif ($aa eq "W"){push(@Wlengths,[@lengths]);}
			elsif ($aa eq "R"){push(@Rlengths,[@lengths]);}
			elsif ($aa eq "G"){push(@Glengths,[@lengths]);}
			elsif ($aa eq "X"){push(@Xlengths,[@lengths]);}
	
		}#if enough sequences collect codons
		
	}#unless total codon counts for GC3 bin, it's 3' stuff; ignore X stop codon lines
	
	elsif ($binID =~ "TOTALS") {
	
		if ($seqs > $SeqCutoff or $seqs == $SeqCutoff){
			
			$codonset = shift @line;
			$cdns = (length $codonset)/3;
			if ($aa eq "F"){push(@Ftotals,$codonset);push(@Ftlengths,$cdns);}
			elsif ($aa eq "L"){push(@Ltotals,$codonset);push(@Ltlengths,$cdns);}
			elsif ($aa eq "I"){push(@Itotals,$codonset);push(@Itlengths,$cdns);}
			elsif ($aa eq "M"){push(@Mtotals,$codonset);push(@Mtlengths,$cdns);}
			elsif ($aa eq "V"){push(@Vtotals,$codonset);push(@Vtlengths,$cdns);}
			elsif ($aa eq "S"){push(@Stotals,$codonset);push(@Stlengths,$cdns);}
			elsif ($aa eq "P"){push(@Ptotals,$codonset);push(@Ptlengths,$cdns);}
			elsif ($aa eq "T"){push(@Ttotals,$codonset);push(@Ttlengths,$cdns);}
			elsif ($aa eq "A"){push(@Atotals,$codonset);push(@Atlengths,$cdns);}
			elsif ($aa eq "Y"){push(@Ytotals,$codonset);push(@Ytlengths,$cdns);}
			elsif ($aa eq "H"){push(@Htotals,$codonset);push(@Htlengths,$cdns);}
			elsif ($aa eq "Q"){push(@Qtotals,$codonset);push(@Qtlengths,$cdns);}
			elsif ($aa eq "N"){push(@Ntotals,$codonset);push(@Ntlengths,$cdns);}
			elsif ($aa eq "K"){push(@Ktotals,$codonset);push(@Ktlengths,$cdns);}
			elsif ($aa eq "D"){push(@Dtotals,$codonset);push(@Dtlengths,$cdns);}
			elsif ($aa eq "E"){push(@Etotals,$codonset);push(@Etlengths,$cdns);}
			elsif ($aa eq "C"){push(@Ctotals,$codonset);push(@Ctlengths,$cdns);}
			elsif ($aa eq "W"){push(@Wtotals,$codonset);push(@Wtlengths,$cdns);}
			elsif ($aa eq "R"){push(@Rtotals,$codonset);push(@Rtlengths,$cdns);}
			elsif ($aa eq "G"){push(@Gtotals,$codonset);push(@Gtlengths,$cdns);}
			elsif ($aa eq "X"){push(@Xtotals,$codonset);push(@Xtlengths,$cdns);}

			
		}#if enough sequences
	
	}#else it's totals for GC3 bin, so collect it to choose 3' codons
}


$sequences = 0;#count all sequences
$badgene = 0;#count "bad" sequences: length not multiple of 3, or contain characters other than ATGC

srand(time|$$);

#now make RRTs:
while ($dnaline = <DNA>){
	if ($dnaline =~ ">"){#collect header & sequence
		$header = $dnaline;
		chomp $header;
		++$sequences;
		$dnaseq = <DNA>;
		chomp $dnaseq;
	
		#Upper case for quality assurance
		$dnaseq = uc $dnaseq;
		#check for complete reading frame and no weird characters
		$dnalength = length $dnaseq;
		$codons = $dnalength/3;
		$otherstuff = $dnaseq;
		$otherstuff =~ s/A//g;$otherstuff =~ s/T//g;$otherstuff =~ s/G//g;$otherstuff =~ s/C//g;
		$lengthstuff = length $otherstuff;#characters other than ATGC ("N", etc)
		
		if ($codons == int($codons) and $lengthstuff == 0){#if length is a multiple of 3 no weird bases
		
			$totalattempts = 0;#total RRTs checked
			$attempts = 0;#RRTs made with current codon usage bin
			$cutoff = $GCcutoff;
			
			#find %GC and %GC3
			$GCseq = $dnaseq;
			$GCseq =~ s/A//g;$GCseq =~ s/T//g;
			$GC = length $GCseq;
			$perGC = 100*($GC/$dnalength);
			
			$GC3 = 0;
			$basenum = 2;#start with 3rd pos of 1st codon
			until ($basenum > $dnalength){
				$base3 = substr($dnaseq,$basenum,1);
				if ($base3 eq "G" or $base3 eq "C"){++$GC3;}
				$basenum = $basenum + 3;	
			}#until last codon
			
			$perGC3 = 100*($GC3/$codons);
							
			$mindif = 100;
			$pos = 0;
			foreach $ave (@GC3Ave){#find codon usage data closest to %GC of current seq
				$dif = $perGC3 - $ave;
				$absdif = ($dif**2)**0.5;#abs value different between %GC of current seq & midpoint of codon usage data
				if ($absdif < $mindif){$mindif = $absdif;$CIbinnum = $pos;}
				++$pos;
			}#foreach set of codon data, find nearest to current seq
			
			$printRRT = 0;#RRTs printed
			$one = 0;#RRTs printed from current CI bin
			
			until ($printRRT == $rrts or $cutoff > $end){#until make required number RRTs or give up
			
				@curRRT = ();
				$cdnstart1 = (3*$codons) - 3;#starting base of last codon
				$codon3p = substr($dnaseq,$cdnstart1,3);#last codon in dnaseq
				#pick the 3' RRT codon from GCbin codon frequencies: no multipliers
				
				if ($codon3p eq "TTT"){
					$randomstart = int(rand($Ftlengths[$CIbinnum]));
					$string = $Ftotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TTC"){
					$randomstart = int(rand($Ftlengths[$CIbinnum]));
					$string = $Ftotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TTA"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TTG"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CTT"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CTC"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CTA"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CTG"){
					$randomstart = int(rand($Ltlengths[$CIbinnum]));
					$string = $Ltotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ATT"){
					$randomstart = int(rand($Itlengths[$CIbinnum]));
					$string = $Itotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ATC"){
					$randomstart = int(rand($Itlengths[$CIbinnum]));
					$string = $Itotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ATA"){
					$randomstart = int(rand($Itlengths[$CIbinnum]));
					$string = $Itotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ATG"){
					$randomstart = int(rand($Mtlengths[$CIbinnum]));
					$string = $Mtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GTT"){
					$randomstart = int(rand($Vtlengths[$CIbinnum]));
					$string = $Vtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GTC"){
					$randomstart = int(rand($Vtlengths[$CIbinnum]));
					$string = $Vtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GTA"){
					$randomstart = int(rand($Vtlengths[$CIbinnum]));
					$string = $Vtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GTG"){
					$randomstart = int(rand($Vtlengths[$CIbinnum]));
					$string = $Vtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TCT"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TCC"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TCA"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TCG"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AGT"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AGC"){
					$randomstart = int(rand($Stlengths[$CIbinnum]));
					$string = $Stotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CCT"){
					$randomstart = int(rand($Ptlengths[$CIbinnum]));
					$string = $Ptotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CCC"){
					$randomstart = int(rand($Ptlengths[$CIbinnum]));
					$string = $Ptotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CCA"){
					$randomstart = int(rand($Ptlengths[$CIbinnum]));
					$string = $Ptotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CCG"){
					$randomstart = int(rand($Ptlengths[$CIbinnum]));
					$string = $Ptotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ACT"){
					$randomstart = int(rand($Ttlengths[$CIbinnum]));
					$string = $Ttotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ACC"){
					$randomstart = int(rand($Ttlengths[$CIbinnum]));
					$string = $Ttotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ACA"){
					$randomstart = int(rand($Ttlengths[$CIbinnum]));
					$string = $Ttotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "ACG"){
					$randomstart = int(rand($Ttlengths[$CIbinnum]));
					$string = $Ttotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GCT"){
					$randomstart = int(rand($Atlengths[$CIbinnum]));
					$string = $Atotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GCC"){
					$randomstart = int(rand($Atlengths[$CIbinnum]));
					$string = $Atotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GCA"){
					$randomstart = int(rand($Atlengths[$CIbinnum]));
					$string = $Atotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GCG"){
					$randomstart = int(rand($Atlengths[$CIbinnum]));
					$string = $Atotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TAT"){
					$randomstart = int(rand($Ytlengths[$CIbinnum]));
					$string = $Ytotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TAC"){
					$randomstart = int(rand($Ytlengths[$CIbinnum]));
					$string = $Ytotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CAT"){
					$randomstart = int(rand($Htlengths[$CIbinnum]));
					$string = $Htotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CAC"){
					$randomstart = int(rand($Htlengths[$CIbinnum]));
					$string = $Htotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CAA"){
					$randomstart = int(rand($Qtlengths[$CIbinnum]));
					$string = $Qtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CAG"){
					$randomstart = int(rand($Qtlengths[$CIbinnum]));
					$string = $Qtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AAT"){
					$randomstart = int(rand($Ntlengths[$CIbinnum]));
					$string = $Ntotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AAC"){
					$randomstart = int(rand($Ntlengths[$CIbinnum]));
					$string = $Ntotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AAA"){
					$randomstart = int(rand($Ktlengths[$CIbinnum]));
					$string = $Ktotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AAG"){
					$randomstart = int(rand($Ktlengths[$CIbinnum]));
					$string = $Ktotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GAT"){
					$randomstart = int(rand($Dtlengths[$CIbinnum]));
					$string = $Dtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GAC"){
					$randomstart = int(rand($Dtlengths[$CIbinnum]));
					$string = $Dtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GAA"){
					$randomstart = int(rand($Etlengths[$CIbinnum]));
					$string = $Etotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GAG"){
					$randomstart = int(rand($Etlengths[$CIbinnum]));
					$string = $Etotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TGT"){
					$randomstart = int(rand($Ctlengths[$CIbinnum]));
					$string = $Ctotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TGC"){
					$randomstart = int(rand($Ctlengths[$CIbinnum]));
					$string = $Ctotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TGG"){
					$randomstart = int(rand($Wtlengths[$CIbinnum]));
					$string = $Wtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CGT"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CGC"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CGA"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "CGG"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AGA"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "AGG"){
					$randomstart = int(rand($Rtlengths[$CIbinnum]));
					$string = $Rtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GGT"){
					$randomstart = int(rand($Gtlengths[$CIbinnum]));
					$string = $Gtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GGC"){
					$randomstart = int(rand($Gtlengths[$CIbinnum]));
					$string = $Gtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GGA"){
					$randomstart = int(rand($Gtlengths[$CIbinnum]));
					$string = $Gtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "GGG"){
					$randomstart = int(rand($Gtlengths[$CIbinnum]));
					$string = $Gtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TAA"){
					$randomstart = int(rand($Xtlengths[$CIbinnum]));
					$string = $Xtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TAG"){
					$randomstart = int(rand($Xtlengths[$CIbinnum]));
					$string = $Xtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				elsif ($codon3p eq "TGA"){
					$randomstart = int(rand($Xtlengths[$CIbinnum]));
					$string = $Xtotals[$CIbinnum];
					$rrt3pcdn = substr($string,(3*$randomstart),3);}
				
				#last codon number indicates which mutliplier to use
				if ($rrt3pcdn eq "TTT"){$lastcodon = 0;}
				elsif ($rrt3pcdn eq "TTC"){$lastcodon = 1;}
				elsif ($rrt3pcdn eq "TTA"){$lastcodon = 2;}
				elsif ($rrt3pcdn eq "TTG"){$lastcodon = 3;}
				elsif ($rrt3pcdn eq "CTT"){$lastcodon = 4;}
				elsif ($rrt3pcdn eq "CTC"){$lastcodon = 5;}
				elsif ($rrt3pcdn eq "CTA"){$lastcodon = 6;}
				elsif ($rrt3pcdn eq "CTG"){$lastcodon = 7;}
				elsif ($rrt3pcdn eq "ATT"){$lastcodon = 8;}
				elsif ($rrt3pcdn eq "ATC"){$lastcodon = 9;}
				elsif ($rrt3pcdn eq "ATA"){$lastcodon = 10;}
				elsif ($rrt3pcdn eq "ATG"){$lastcodon = 11;}
				elsif ($rrt3pcdn eq "GTT"){$lastcodon = 12;}
				elsif ($rrt3pcdn eq "GTC"){$lastcodon = 13;}
				elsif ($rrt3pcdn eq "GTA"){$lastcodon = 14;}
				elsif ($rrt3pcdn eq "GTG"){$lastcodon = 15;}
				elsif ($rrt3pcdn eq "TCT"){$lastcodon = 16;}
				elsif ($rrt3pcdn eq "TCC"){$lastcodon = 17;}
				elsif ($rrt3pcdn eq "TCA"){$lastcodon = 18;}
				elsif ($rrt3pcdn eq "TCG"){$lastcodon = 19;}
				elsif ($rrt3pcdn eq "AGT"){$lastcodon = 20;}
				elsif ($rrt3pcdn eq "AGC"){$lastcodon = 21;}
				elsif ($rrt3pcdn eq "CCT"){$lastcodon = 22;}
				elsif ($rrt3pcdn eq "CCC"){$lastcodon = 23;}
				elsif ($rrt3pcdn eq "CCA"){$lastcodon = 24;}
				elsif ($rrt3pcdn eq "CCG"){$lastcodon = 25;}
				elsif ($rrt3pcdn eq "ACT"){$lastcodon = 26;}
				elsif ($rrt3pcdn eq "ACC"){$lastcodon = 27;}
				elsif ($rrt3pcdn eq "ACA"){$lastcodon = 28;}
				elsif ($rrt3pcdn eq "ACG"){$lastcodon = 29;}
				elsif ($rrt3pcdn eq "GCT"){$lastcodon = 30;}
				elsif ($rrt3pcdn eq "GCC"){$lastcodon = 31;}
				elsif ($rrt3pcdn eq "GCA"){$lastcodon = 32;}
				elsif ($rrt3pcdn eq "GCG"){$lastcodon = 33;}
				elsif ($rrt3pcdn eq "TAT"){$lastcodon = 34;}
				elsif ($rrt3pcdn eq "TAC"){$lastcodon = 35;}
				elsif ($rrt3pcdn eq "CAT"){$lastcodon = 36;}
				elsif ($rrt3pcdn eq "CAC"){$lastcodon = 37;}
				elsif ($rrt3pcdn eq "CAA"){$lastcodon = 38;}
				elsif ($rrt3pcdn eq "CAG"){$lastcodon = 39;}
				elsif ($rrt3pcdn eq "AAT"){$lastcodon = 40;}
				elsif ($rrt3pcdn eq "AAC"){$lastcodon = 41;}
				elsif ($rrt3pcdn eq "AAA"){$lastcodon = 42;}
				elsif ($rrt3pcdn eq "AAG"){$lastcodon = 43;}
				elsif ($rrt3pcdn eq "GAT"){$lastcodon = 44;}
				elsif ($rrt3pcdn eq "GAC"){$lastcodon = 45;}
				elsif ($rrt3pcdn eq "GAA"){$lastcodon = 46;}
				elsif ($rrt3pcdn eq "GAG"){$lastcodon = 47;}
				elsif ($rrt3pcdn eq "TGT"){$lastcodon = 48;}
				elsif ($rrt3pcdn eq "TGC"){$lastcodon = 49;}
				elsif ($rrt3pcdn eq "TGG"){$lastcodon = 50;}
				elsif ($rrt3pcdn eq "CGT"){$lastcodon = 51;}
				elsif ($rrt3pcdn eq "CGC"){$lastcodon = 52;}
				elsif ($rrt3pcdn eq "CGA"){$lastcodon = 53;}
				elsif ($rrt3pcdn eq "CGG"){$lastcodon = 54;}
				elsif ($rrt3pcdn eq "AGA"){$lastcodon = 55;}
				elsif ($rrt3pcdn eq "AGG"){$lastcodon = 56;}
				elsif ($rrt3pcdn eq "GGT"){$lastcodon = 57;}
				elsif ($rrt3pcdn eq "GGC"){$lastcodon = 58;}
				elsif ($rrt3pcdn eq "GGA"){$lastcodon = 59;}
				elsif ($rrt3pcdn eq "GGG"){$lastcodon = 60;}
				elsif ($rrt3pcdn eq "TAA"){$lastcodon = 61;}
				elsif ($rrt3pcdn eq "TAG"){$lastcodon = 62;}
				elsif ($rrt3pcdn eq "TGA"){$lastcodon = 63;}
				
				#add 3' codon to RRT seq
				unshift (@curRRT,$rrt3pcdn);
				
				#start of next codon to choose RRT for:
				$cdnstart = $cdnstart1 - 3;
				
				until ($cdnstart < 0){#until 5' codon
				
					$codon = substr($dnaseq,$cdnstart,3);
					
					if ($codon eq "TTT"){
						$random = int(rand($Flengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($F[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TTC"){
						$random = int(rand($Flengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($F[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TTA"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TTG"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CTT"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CTC"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CTA"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CTG"){
						$random = int(rand($Llengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($L[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ATT"){
						$random = int(rand($Ilengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($I[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ATC"){
						$random = int(rand($Ilengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($I[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ATA"){
						$random = int(rand($Ilengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($I[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ATG"){
						$random = int(rand($Mlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($M[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GTT"){
						$random = int(rand($Vlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($V[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GTC"){
						$random = int(rand($Vlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($V[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GTA"){
						$random = int(rand($Vlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($V[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GTG"){
						$random = int(rand($Vlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($V[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TCT"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TCC"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TCA"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TCG"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AGT"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AGC"){
						$random = int(rand($Slengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($S[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CCT"){
						$random = int(rand($Plengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($P[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CCC"){
						$random = int(rand($Plengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($P[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CCA"){
						$random = int(rand($Plengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($P[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CCG"){
						$random = int(rand($Plengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($P[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ACT"){
						$random = int(rand($Tlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($T[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ACC"){
						$random = int(rand($Tlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($T[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ACA"){
						$random = int(rand($Tlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($T[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "ACG"){
						$random = int(rand($Tlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($T[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GCT"){
						$random = int(rand($Alengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($A[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GCC"){
						$random = int(rand($Alengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($A[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GCA"){
						$random = int(rand($Alengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($A[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GCG"){
						$random = int(rand($Alengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($A[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TAT"){
						$random = int(rand($Ylengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($Y[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TAC"){
						$random = int(rand($Ylengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($Y[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CAT"){
						$random = int(rand($Hlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($H[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CAC"){
						$random = int(rand($Hlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($H[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CAA"){
						$random = int(rand($Qlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($Q[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CAG"){
						$random = int(rand($Qlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($Q[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AAT"){
						$random = int(rand($Nlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($N[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AAC"){
						$random = int(rand($Nlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($N[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AAA"){
						$random = int(rand($Klengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($K[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AAG"){
						$random = int(rand($Klengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($K[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GAT"){
						$random = int(rand($Dlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($D[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GAC"){
						$random = int(rand($Dlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($D[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GAA"){
						$random = int(rand($Elengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($E[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GAG"){
						$random = int(rand($Elengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($E[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TGT"){
						$random = int(rand($Clengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($C[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TGC"){
						$random = int(rand($Clengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($C[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TGG"){
						$random = int(rand($Wlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($W[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CGT"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CGC"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CGA"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "CGG"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AGA"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "AGG"){
						$random = int(rand($Rlengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($R[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GGT"){
						$random = int(rand($Glengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($G[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GGC"){
						$random = int(rand($Glengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($G[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GGA"){
						$random = int(rand($Glengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($G[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "GGG"){
						$random = int(rand($Glengths[$CIbinnum][$lastcodon]));
						$rrtcodon = substr($G[$CIbinnum][$lastcodon],(3*$random),3);}
					#elsif ($codon eq "TAA"){$random = int(rand($Xlengths[$CIbinnum][$lastcodon]));$rrtcodon = substr($X[$CIbinnum][$lastcodon],(3*$random),3);}
					#elsif ($codon eq "TAG"){$random = int(rand($Xlengths[$CIbinnum][$lastcodon]));$rrtcodon = substr($X[$CIbinnum][$lastcodon],(3*$random),3);}
					#elsif ($codon eq "TGA"){$random = int(rand($Xlengths[$CIbinnum][$lastcodon]));$rrtcodon = substr($X[$CIbinnum][$lastcodon],(3*$random),3);}
					elsif ($codon eq "TAA"){$rrtcodon = $codon;}#internal stop codons are kept
					elsif ($codon eq "TAG"){$rrtcodon = $codon;}
					elsif ($codon eq "TGA"){$rrtcodon = $codon;}
					
					if ($rrtcodon eq "TTT"){$lastcodon = 0;}
					elsif ($rrtcodon eq "TTC"){$lastcodon = 1;}
					elsif ($rrtcodon eq "TTA"){$lastcodon = 2;}
					elsif ($rrtcodon eq "TTG"){$lastcodon = 3;}
					elsif ($rrtcodon eq "CTT"){$lastcodon = 4;}
					elsif ($rrtcodon eq "CTC"){$lastcodon = 5;}
					elsif ($rrtcodon eq "CTA"){$lastcodon = 6;}
					elsif ($rrtcodon eq "CTG"){$lastcodon = 7;}
					elsif ($rrtcodon eq "ATT"){$lastcodon = 8;}
					elsif ($rrtcodon eq "ATC"){$lastcodon = 9;}
					elsif ($rrtcodon eq "ATA"){$lastcodon = 10;}
					elsif ($rrtcodon eq "ATG"){$lastcodon = 11;}
					elsif ($rrtcodon eq "GTT"){$lastcodon = 12;}
					elsif ($rrtcodon eq "GTC"){$lastcodon = 13;}
					elsif ($rrtcodon eq "GTA"){$lastcodon = 14;}
					elsif ($rrtcodon eq "GTG"){$lastcodon = 15;}
					elsif ($rrtcodon eq "TCT"){$lastcodon = 16;}
					elsif ($rrtcodon eq "TCC"){$lastcodon = 17;}
					elsif ($rrtcodon eq "TCA"){$lastcodon = 18;}
					elsif ($rrtcodon eq "TCG"){$lastcodon = 19;}
					elsif ($rrtcodon eq "AGT"){$lastcodon = 20;}
					elsif ($rrtcodon eq "AGC"){$lastcodon = 21;}
					elsif ($rrtcodon eq "CCT"){$lastcodon = 22;}
					elsif ($rrtcodon eq "CCC"){$lastcodon = 23;}
					elsif ($rrtcodon eq "CCA"){$lastcodon = 24;}
					elsif ($rrtcodon eq "CCG"){$lastcodon = 25;}
					elsif ($rrtcodon eq "ACT"){$lastcodon = 26;}
					elsif ($rrtcodon eq "ACC"){$lastcodon = 27;}
					elsif ($rrtcodon eq "ACA"){$lastcodon = 28;}
					elsif ($rrtcodon eq "ACG"){$lastcodon = 29;}
					elsif ($rrtcodon eq "GCT"){$lastcodon = 30;}
					elsif ($rrtcodon eq "GCC"){$lastcodon = 31;}
					elsif ($rrtcodon eq "GCA"){$lastcodon = 32;}
					elsif ($rrtcodon eq "GCG"){$lastcodon = 33;}
					elsif ($rrtcodon eq "TAT"){$lastcodon = 34;}
					elsif ($rrtcodon eq "TAC"){$lastcodon = 35;}
					elsif ($rrtcodon eq "CAT"){$lastcodon = 36;}
					elsif ($rrtcodon eq "CAC"){$lastcodon = 37;}
					elsif ($rrtcodon eq "CAA"){$lastcodon = 38;}
					elsif ($rrtcodon eq "CAG"){$lastcodon = 39;}
					elsif ($rrtcodon eq "AAT"){$lastcodon = 40;}
					elsif ($rrtcodon eq "AAC"){$lastcodon = 41;}
					elsif ($rrtcodon eq "AAA"){$lastcodon = 42;}
					elsif ($rrtcodon eq "AAG"){$lastcodon = 43;}
					elsif ($rrtcodon eq "GAT"){$lastcodon = 44;}
					elsif ($rrtcodon eq "GAC"){$lastcodon = 45;}
					elsif ($rrtcodon eq "GAA"){$lastcodon = 46;}
					elsif ($rrtcodon eq "GAG"){$lastcodon = 47;}
					elsif ($rrtcodon eq "TGT"){$lastcodon = 48;}
					elsif ($rrtcodon eq "TGC"){$lastcodon = 49;}
					elsif ($rrtcodon eq "TGG"){$lastcodon = 50;}
					elsif ($rrtcodon eq "CGT"){$lastcodon = 51;}
					elsif ($rrtcodon eq "CGC"){$lastcodon = 52;}
					elsif ($rrtcodon eq "CGA"){$lastcodon = 53;}
					elsif ($rrtcodon eq "CGG"){$lastcodon = 54;}
					elsif ($rrtcodon eq "AGA"){$lastcodon = 55;}
					elsif ($rrtcodon eq "AGG"){$lastcodon = 56;}
					elsif ($rrtcodon eq "GGT"){$lastcodon = 57;}
					elsif ($rrtcodon eq "GGC"){$lastcodon = 58;}
					elsif ($rrtcodon eq "GGA"){$lastcodon = 59;}
					elsif ($rrtcodon eq "GGG"){$lastcodon = 60;}
					elsif ($rrtcodon eq "TAA"){$lastcodon = 61;}
					elsif ($rrtcodon eq "TAG"){$lastcodon = 62;}
					elsif ($rrtcodon eq "TGA"){$lastcodon = 63;}
				
					unshift(@curRRT,$rrtcodon);
					
					$cdnstart = $cdnstart - 3;
					
				}#until last codon: make current RRT
				
				++$attempts;++$totalattempts;
				
				
				$RRTseq = join('', @curRRT);
				$RRTcopy = $RRTseq;
				
				$RRTcopy =~ s/A//g;$RRTcopy =~ s/T//g;
				$rrtGC = length $RRTcopy;
				
				$prrtGC = 100*($rrtGC/$dnalength);
				$GCdif = $prrtGC - $perGC;
				
				$avGCdif = ($GCdif**2)**0.5;
				
				if ($avGCdif < 1 or $avGCdif == 1){
					print OUTPUT $header,"\n",$RRTseq,"\n";
					++$printRRT;++$one;
				}#within GC range		
								
				if ($attempts == 5 and $printRRT == 0){#if made several attempts with current CI and no good RRTs, change to dif CI
									
					if ($GCdif > 0){
						if ($CIbinnum - 1 > 0){$CIbinnum = $CIbinnum - 1;$attempts = 0;}
					}#current CI producing RRTs with %GC too high
					elsif ($GCdif < 0){
						if ($CIbinnum + 1 < $CIbins){$CIbinnum = $CIbinnum + 1;$attempts = 0;}
					}#current CI producing RRTs with %GC too low
									
				}#if not getting close %GC with current CI
				
				if ($totalattempts > $max_attempts){++$cutoff;$totalattempts = 0;}
				
			}#until required number of RRTs
			
			if ($cutoff > $GCcutoff ){print "$header $cutoff\n";}
		}#if full frame & no weird bases
		
		else {
			++$badgene;
		}#else bad seq
	
	}#if starting new seq
}


close DNA;
exit;
